package com.ccp.dev.qbdms.util;

import com.google.common.collect.Maps;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.util.*;

/**
 * ClassDescribe:
 *
 * @author :milihao
 * @date :2019-06-23 Since:1
 */
public class ProcessingTimeUtil {

	private static final String YEAR = "andtj";
	private static final String MONTH = "aydtj";

	/**
	 * 2020-6-23 zzy 修改
	 * @param type
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static List<Map<String, Object>> packagingTime(String type, Date startTime, Date endTime) {
		if (StringUtils.isBlank(type) || null == startTime || null == endTime) {
			return Lists.newArrayList();
		}
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(startTime);
		int startYear = startCalendar.get(Calendar.YEAR);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endTime);
		int endYear = endCalendar.get(Calendar.YEAR);
		List<Map<String, Object>> returnMapList = Lists.newArrayList();
		Map<String, Object> timeMap;
		if (type.equals(YEAR)) {
			int length = endYear - startYear + 1;
			for (int i = 0; i < length; i++) {
				timeMap = Maps.newHashMap();
				try {
					timeMap.put("startTime",
							DateUtils.parseDate((startYear + i) + "-" + "01-01 00:00:00", "yyyy-MM-dd HH:mm:ss"));
					timeMap.put("endTime",
							DateUtils.parseDate((startYear + i) + "-" + "12-31 23:59:59", "yyyy-MM-dd HH:mm:ss"));
					timeMap.put("title",(startYear + i));
				} catch (ParseException e) {
					e.printStackTrace();
				}
				returnMapList.add(timeMap);
			}
			return returnMapList;
		}
		if (type.equals(MONTH)) {
			int startMonth = startCalendar.get(Calendar.MONTH);
			int endMonth = endCalendar.get(Calendar.MONTH);
			int length = (endYear - startYear) * 12 + (endMonth - startMonth) + 1;
			Calendar temp = startCalendar;
			for (int i = 0; i < length; i++) {
				timeMap = Maps.newHashMap();
				int m = temp.get(Calendar.MONTH)+1;
				int y = temp.get(Calendar.YEAR);
				temp.getActualMaximum(Calendar.DAY_OF_MONTH);
				try {
					timeMap.put("startTime",
							DateUtils
									.parseDate(y + "-" + m + "-01 00:00:00", "yyyy-MM-dd HH:mm:ss"));
					timeMap.put("endTime",
							DateUtils
									.parseDate(y + "-" + m + "-"+temp.getActualMaximum(Calendar.DAY_OF_MONTH)+" 23:59:59", "yyyy-MM-dd HH:mm:ss"));
					timeMap.put("title",y+"-"+m);

				} catch (ParseException e) {
					e.printStackTrace();
				}
				temp.add(Calendar.MONTH, 1);
				returnMapList.add(timeMap);
			}
			return returnMapList;
		}
		return returnMapList;
	}

	/**
	 * 用来转换简单的时间范围
	 * 如:传入 2020-06-01 2020-06-05
	 * 会转为 2020-06-01 00:00:00  2020-06-05 23:59:59
	 * 2020-6-24 zzy
	 * @auth zzy
	 * @param type 指定按年还是按月
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static Map<String,Object> converSimpleDateRange(String type,Date startTime, Date endTime){
		Map<String, Object> querymap = Maps.newHashMap();
		Calendar startc = Calendar.getInstance();
		Calendar endc = Calendar.getInstance();
		startc.setTime(startTime);
		endc.setTime(endTime);
		startc.set(Calendar.HOUR_OF_DAY, 0);
		startc.set(Calendar.MINUTE, 0);
		startc.set(Calendar.SECOND, 0);
		endc.set(Calendar.HOUR_OF_DAY, 23);
		endc.set(Calendar.MINUTE, 59);
		endc.set(Calendar.SECOND, 59);
		if (YEAR.equals(type)) {
			startc.set(Calendar.MONTH,0);
			startc.set(Calendar.DAY_OF_MONTH,1);
			endc.set(Calendar.MONTH,11);
			endc.set(Calendar.DAY_OF_MONTH,31);
		}
		else if (MONTH.equals(type)) {
			startc.set(Calendar.DAY_OF_MONTH,1);
			endc.set(Calendar.DAY_OF_MONTH,endc.getActualMaximum(Calendar.DAY_OF_MONTH));
		}
		querymap.put("startTime", startc.getTime());
		querymap.put("endTime", endc.getTime());
		return querymap;
	}

	/**
	 * 用来转换 xx年至xx年 这种字符串,用于统计图的标题处理
	 * 2020-6-28 zzy
	 * @auth zzy
	 * @param type
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static String converDateToTitleStr(String type, Date startTime, Date endTime){
		Calendar startc = Calendar.getInstance();
		startc.setTime(startTime);
		Calendar endc = Calendar.getInstance();
		endc.setTime(endTime);
		if (YEAR.equals(type)) {
			return startc.get(Calendar.YEAR)+"年至"+endc.get(Calendar.YEAR)+"年";
		}
		else if(MONTH.equals(type)){
			return startc.get(Calendar.YEAR)+"年"+(startc.get(Calendar.MONTH)+1)+"月至"+endc.get(Calendar.YEAR)+"年"+(endc.get(Calendar.MONTH)+1)+"月";
		}
		return "";
	}

}
